package com.hyt.it.ogt.kq.service.gov.api.bm;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.apiInfor.DataType;
import com.hyt.common.apiInfor.ParamType;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.service.gov.model.vo.TimeRoomCandidateVO;
import com.hyt.it.ogt.kq.service.gov.service.ISubjectService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskOverviewService;
import com.hyt.it.ogt.kq.service.gov.service.ITimeRoomCandidateService;
import com.hyt.it.ogt.kq.service.model.entity.Subject;
import com.hyt.it.ogt.kq.service.model.param.PushCandidateSubjectParam;
import com.hyt.it.ogt.kq.service.model.vo.wechat.WeChatCandidateArrangeDetailVO;
import com.hyt.it.ogt.kq.service.model.vo.wechat.WeChatCandidateArrangeVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 报名服务调用的接口
 * </p>
 *
 * @author liuq
 * @since 2021-08-02
 */
@RestController
@Api(tags = "24.报名服务调用的接口", value = "报名服务调用的接口")
@ApiSort(value = 24)
@RequestMapping("/api/bm")
@Slf4j
public class BmApi {

	@Autowired
    private ISubjectService iSubjectService;

	@Autowired
	private ITaskOverviewService iTaskOverviewService;

	@Resource
	private ITimeRoomCandidateService iTimeRoomCandidateService;
	
    @ApiOperation(
            value = "24.1 编辑科目",
            notes = "30000:成功"
    )
    @ApiOperationSort(value = 1)
    @RequestMapping(value = "/subject/edit", method = RequestMethod.PUT)
    public ApiResponse<Object> editSubject(@RequestParam("subjectJson") String subjectJson) {
    	Subject subject = JSONObject.parseObject(subjectJson, Subject.class);
    	subject.setId("1000");
    	iSubjectService.updateById(subject);
    	return ApiResponse.builder()
                .code(ResponseCode.SUCCESS_TEMP.getCode())
                .build();
    }
    
	@ApiOperation(
			value = "24.6 查询考生编排数据",
			notes = "30000:成功"
	)
	@ApiOperationSort(value = 6)
	@RequestMapping(value = "/get/getAdmissionInfo", method = RequestMethod.GET)
	public ApiResponse<List<TimeRoomCandidateVO>> getAdmissionInfo(String projectId, String studentId) {
		log.info("# 查询考生编排数据参数 ： {},{}", projectId,studentId);
		try {
			return ApiResponse.<List<TimeRoomCandidateVO>>builder()
					.code(ResponseCode.SUCCESS_TEMP.getCode())
					.data(iTaskOverviewService.getAdmissionInfo(projectId,studentId))
					.build();
		} catch (KqException i) {
			log.error("# 查询考生编排数据异常: {}", i.getErrMsg());
			return ApiResponse.<List<TimeRoomCandidateVO>>builder()
					.code(i.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("# 查询考生编排数据异常:", e);
			return ApiResponse.<List<TimeRoomCandidateVO>>builder()
					.code(ResponseCode.ERROR_BM_API_GET_CANDIDATE_INFO.getCode())
					.build();
		}
	}

	@ApiOperation(
			value = "24.7 推送考生科目",
			notes = "30000:成功"
	)
	@ApiOperationSort(value = 7)
	@RequestMapping(value = "/push/candidateSubject", method = RequestMethod.POST)
	public ApiResponse<Object> pushCandidateSubject(@RequestBody PushCandidateSubjectParam param) {
		log.info("# 推送考生科目参数 ： {}", param);
		try {
			//推送。
			iTaskOverviewService.pushCandidateSubject(param);
			return ApiResponse.builder()
					.code(ResponseCode.SUCCESS_TEMP.getCode())
					.build();
		} catch (KqException i) {
			log.error("# 推送考生科目异常: {}", i.getErrMsg());
			return ApiResponse.builder()
					.code(i.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("# 推送考生科目异常:", e);
			return ApiResponse.builder()
					.code(ResponseCode.ERROR_BM_API_PUSH_CANDIDATE_SUBJECT.getCode())
					.build();
		}
	}

	@ApiOperation(
			value = "24.9 根据报名id、报名考生id获取微信考生考试安排（针对单个报名任务）",
			notes = "30000:成功"
	)
	@ApiOperationSort(value = 9)
	@ApiImplicitParams({
			@ApiImplicitParam(
					name = "signUpId",
					value = "报名id",
					dataType = DataType.STRING,
					paramType = ParamType.QUERY,
					required = true
			),
			@ApiImplicitParam(
					name = "studentId",
					value = "报名考生id",
					dataType = DataType.STRING,
					paramType = ParamType.QUERY,
					required = true
			)
	})
	@GetMapping("/get/getWeChatCandidateArrangeVO")
	public ApiResponse<List<WeChatCandidateArrangeVO>> getWeChatCandidateArrangeVO(@RequestParam String signUpId,
																				 @RequestParam String studentId) {
		log.info("# 根据报名id、报名考生id获取微信考生考试安排（针对单个报名任务）参数: {}, {}", signUpId, studentId);
		try {
			List<WeChatCandidateArrangeVO> WeChatCandidateArrangeVOList =
					iTimeRoomCandidateService.getWeChatCandidateArrangeVO(signUpId, studentId);
			return ApiResponse.<List<WeChatCandidateArrangeVO>>builder()
					.code(ResponseCode.SUCCESS.getCode())
					.data(WeChatCandidateArrangeVOList)
					.build();
		} catch (KqException k) {
			log.error("# 根据报名id、报名考生id获取微信考生考试安排（针对单个报名任务）业务异常: {}", k.getErrMsg());
			return ApiResponse.<List<WeChatCandidateArrangeVO>>builder()
					.code(k.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("# 根据报名id、报名考生id获取微信考生考试安排（针对单个报名任务）异常:", e);
			return ApiResponse.<List<WeChatCandidateArrangeVO>>builder()
					.code(ResponseCode.GET_WECHAT_CANDIDATE_ARRANGE_EXCEPTION.getCode())
					.build();
		}
	}

	@ApiOperation(
			value = "24.10 根据多个报名考生id获取微信考生考试安排（针对多个报名任务）",
			notes = "30000:成功"
	)
	@ApiOperationSort(value = 10)
	@ApiImplicitParams({
			@ApiImplicitParam(
					name = "studentIds",
					value = "报名考生id（多个用英文逗号分隔）",
					dataType = DataType.STRING,
					paramType = ParamType.QUERY,
					required = true
			)
	})
	@GetMapping("/get/getWeChatCandidateArrangeVOByStudentIds")
	public ApiResponse<List<WeChatCandidateArrangeVO>> getWeChatCandidateArrangeVOByStudentIds(@RequestParam String studentIds) {
		log.info("# 根据多个报名考生id获取微信考生考试安排（针对多个报名任务）参数: {}", studentIds);
		try {
			List<WeChatCandidateArrangeVO> weChatCandidateArrangeVOList = iTimeRoomCandidateService.getWeChatCandidateArrangeVO(studentIds);
			//新疆自定义编排数据 需要两个数据组合起来传给报名，因为这两部分数据存放位置不一样。
			if (weChatCandidateArrangeVOList == null) {
			    weChatCandidateArrangeVOList = new ArrayList<>();
			}
			return ApiResponse.<List<WeChatCandidateArrangeVO>>builder()
					.code(ResponseCode.SUCCESS.getCode())
					.data(weChatCandidateArrangeVOList)
					.build();
		} catch (KqException k) {
			log.error("# 根据多个报名考生id获取微信考生考试安排（针对多个报名任务）业务异常: {}", k.getErrMsg());
			return ApiResponse.<List<WeChatCandidateArrangeVO>>builder()
					.code(k.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("# 根据多个报名考生id获取微信考生考试安排（针对多个报名任务）异常:", e);
			return ApiResponse.<List<WeChatCandidateArrangeVO>>builder()
					.code(ResponseCode.GET_BATCH_WECHAT_CANDIDATE_ARRANGE_EXCEPTION.getCode())
					.build();
		}
	}

	@ApiOperation(
			value = "24.11 获取微信考生考试安排详情",
			notes = "30000:成功"
	)
	@ApiOperationSort(value = 11)
	@ApiImplicitParams({
			@ApiImplicitParam(
					name = "signUpId",
					value = "报名id",
					dataType = DataType.STRING,
					paramType = ParamType.QUERY,
					required = true
			),
			@ApiImplicitParam(
					name = "timeId",
					value = "批次id",
					dataType = DataType.STRING,
					paramType = ParamType.QUERY,
					required = true
			),
			@ApiImplicitParam(
					name = "studentId",
					value = "报名考生id",
					dataType = DataType.STRING,
					paramType = ParamType.QUERY,
					required = true
			)
	})
	@GetMapping("/get/getWeChatCandidateArrangeDetailVO")
	public ApiResponse<WeChatCandidateArrangeDetailVO> getWeChatCandidateArrangeDetailVO(@RequestParam String signUpId,
																					   @RequestParam String timeId,
																					   @RequestParam String studentId) {
		log.info("# 获取微信考生考试安排详情参数: {}, {}, {}", signUpId, timeId, studentId);
		try {
			WeChatCandidateArrangeDetailVO WeChatCandidateArrangeDetailVO =
					iTimeRoomCandidateService.getWeChatCandidateArrangeDetailVO(signUpId, timeId, studentId);

			if (WeChatCandidateArrangeDetailVO == null) {
				return ApiResponse.<WeChatCandidateArrangeDetailVO>builder()
						.code(ResponseCode.GET_WECHAT_CANDIDATE_ARRANGE_DETAIL_NONE_EXCEPTION.getCode())
						.build();
			}

			return ApiResponse.<WeChatCandidateArrangeDetailVO>builder()
					.code(ResponseCode.SUCCESS.getCode())
					.data(WeChatCandidateArrangeDetailVO)
					.build();
		} catch (KqException k) {
			log.error("# 获取微信考生考试安排详情业务异常: {}", k.getErrMsg());
			return ApiResponse.<WeChatCandidateArrangeDetailVO>builder()
					.code(k.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("# 获取微信考生考试安排详情异常:", e);
			return ApiResponse.<WeChatCandidateArrangeDetailVO>builder()
					.code(ResponseCode.GET_WECHAT_CANDIDATE_ARRANGE_DETAIL_EXCEPTION.getCode())
					.build();
		}
	}

}
